package open.tech.dawn.rest.common.page;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import open.tech.dawn.web.util.HttpKit;
import open.tech.dawn.web.util.ToolUtil;

import javax.servlet.http.HttpServletRequest;

public class PageFactory<T> {

    public Page<T> create(PageQuery pageQuery) {
        HttpServletRequest request = HttpKit.getRequest();
        int limit = pageQuery.getLimit();     //每页多少条数据
        int pageNo = pageQuery.getPage();
        String sort = pageQuery.getField();       //排序字段名称
        sort = StringUtils.camelToUnderline(sort);//驼峰转换为下划线
        String order = pageQuery.getOrder();
        if (ToolUtil.isEmpty(sort)) {
            Page<T> page = new Page<>(pageNo, limit);
            return page;
        } else {
            Page<T> page = new Page<>(pageNo, limit);
            if (Order.ASC.getDes().equals(order)) {
                page.addOrder(OrderItem.asc(StringUtils.camelToUnderline(sort)));
            } else {
                page.addOrder(OrderItem.desc(StringUtils.camelToUnderline(sort)));
            }
            return page;
        }
    }
}
